package okhttp;

import java.io.File;

import okhttp3.internal.cache.DiskLruCache;
import okhttp3.internal.cache.DiskLruCache.Editor;
import okhttp3.internal.io.FileSystem;
import okio.Buffer;
import okio.Okio;
import okio.Sink;
import okio.Source;

public class DiskLruCacheDemo {

	// 一个完整的追加事务的过程
	private static void test(DiskLruCache cache ,String tag) throws Exception {
        Editor editor = cache.edit(tag);//开启名字为 tag 的事务
        File file = new File("E:/zsl/temp2.txt");//temp2.txt 占用1303个字节
        Buffer buffer = new Buffer();
        Source source = Okio.source(file);
        source.read(buffer, file.length());
        source.close();
        Sink sink = editor.newSink(0);
        sink.write(buffer, buffer.size());
        sink.close();
        editor.commit();
    }

	public static void main(String[] args) throws Exception {
		// test code
		DiskLruCache cache = DiskLruCache.create(FileSystem.SYSTEM, new File("E:/zsl/cache"), 1, 1, 3000);
		cache.initialize();
		//test(cache, "hello1");
		//test(cache, "hello2");
		//test(cache, "hello3");
		//test(cache, "hello4");
		test(cache, "hello5");

		DiskLruCache.Snapshot shot = cache.get("hello5");
		Thread.sleep(1000000);
	}

}
